package com.ruihang.generalibrary.utils;

import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;

import javax.crypto.KeyGenerator;
import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

/**
 * Created by liuyu on 2017/8/11.
 */

public class HmacSHA256Utils {

    public static String digest(String key,String content){
        try {
//            KeyGenerator keyGenerator=KeyGenerator.getInstance("HmacSHA256");
            Mac msc=Mac.getInstance("HmacSHA256");
            byte[] secreByte=key.getBytes("utf-8");
            byte[] dataBytes=content.getBytes("utf-8");

            SecretKey secret=new SecretKeySpec(secreByte,"HmacSHA256");
            msc.init(secret);
            byte[] dofinal=msc.doFinal(dataBytes);
//            byte[] hexB=HexUtil.encode(dofinal);
            return HexUtil.encode(dofinal);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String digestD(String key,String content){
        try {
            KeyGenerator keyGenerator=KeyGenerator.getInstance("HmacSHA256");
            SecretKey secretKey=keyGenerator.generateKey();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return null;
    }

}
